package cn.breeze.order.service.impl;

import cn.breeze.basic.dto.HandlerMsgDto;
import cn.breeze.basic.dto.HandlerServeDto;
import cn.breeze.basic.exception.BusinessException;
import cn.breeze.basic.utils.CodeGenerateUtils;
import cn.breeze.basic.utils.LoginContext;
import cn.breeze.order.domain.OrderPetAcquisition;
import cn.breeze.order.domain.OrderProduct;
import cn.breeze.order.domain.OrderProductDetail;
import cn.breeze.order.domain.ProductOrderLog;
import cn.breeze.order.dto.ConfirmOrderDto;
import cn.breeze.order.mapper.OrderProductDetailMapper;
import cn.breeze.order.mapper.OrderProductMapper;
import cn.breeze.order.service.IOrderProductService;
import cn.breeze.basic.service.impl.BaseServiceImpl;
import cn.breeze.pet.domain.Pet;
import cn.breeze.pet.domain.SearchMasterMsg;
import cn.breeze.user.domain.User;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.OrderUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author breeze
 * @since 2022-07-21
 */
@Service
public class OrderProductServiceImpl extends BaseServiceImpl<OrderProduct> implements IOrderProductService {

    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private OrderProductDetailMapper orderProductDetailMapper;

    // 生成产品订单
    @Override
    public void save(OrderProduct orderProduct, HttpServletRequest request) {
        // 判断参数非空
        if (orderProduct.getProductId() == null){
            throw new BusinessException("参数异常！");
        }
        // 初始化订单信息
        // 获取订单编号
        Long productId = orderProduct.getProductId();
        String orderSn = CodeGenerateUtils.generateOrderSn(productId);
        orderProduct.setOrderSn(orderSn);
        orderProduct.setLastPayTime(new Date(System.currentTimeMillis() + 30*60*1000));
        // 获取用户id
        User user = LoginContext.getUser(request);
        orderProduct.setUserId(user.getId());
        // 设置订单状态
        orderProduct.setState(0);
        // 保存订单
        orderProductMapper.save(orderProduct);
    }

    @Override
    public void accept(HandlerServeDto handlerServeDto) {
        Long handler = handlerServeDto.getHandler();
        Long orderId = handlerServeDto.getOrderId();
        String note = handlerServeDto.getNote();
        // 判断参非空
        if (handler == null || orderId == null || StringUtils.isEmpty(note)){
            throw new BusinessException("参数非法，请重新传递！");
        }
        // 判断该消息是否已被接收
        // 格局消息id去查询订单详情是否存在
        OrderProductDetail orderProductDetail1 = orderProductDetailMapper.loadByOrderId(orderId);
        if (orderProductDetail1 != null){
            throw new BusinessException("该服务已被处理！");
        }
        OrderProductDetail orderProductDetail = initOrderProductDetail(orderId);
        // 保存订单详情
        orderProductDetailMapper.save(orderProductDetail);
        // 修改订单状态为2，待处理
        OrderProduct orderProduct = orderProductMapper.loadById(orderId);
        orderProduct.setState(2);
        orderProductMapper.update(orderProduct);


    }

    // 初始化orderProductDetail
    private OrderProductDetail initOrderProductDetail(Long orderId) {
        OrderProduct orderProduct = orderProductMapper.loadById(orderId);
        OrderProductDetail orderProductDetail = new OrderProductDetail();
        orderProductDetail.setName(orderProduct.getDigest());
        orderProductDetail.setSaleprice(orderProduct.getPrice());
        orderProductDetail.setProductId(orderProduct.getProductId());
        orderProductDetail.setOrderId(orderProduct.getId());
        return orderProductDetail;
    }

    @Override
    public void reject(Long id) {
        // 根据id去查询
        OrderProduct orderProduct = orderProductMapper.loadById(id);
        if (orderProduct == null){
            throw new BusinessException("非法参数！");
        }
        orderProduct.setShopId(null);
        // 修改店铺id为null，将当前消息放入到池中
        orderProductMapper.update(orderProduct);
    }

    @Override
    public void goConfirm(ConfirmOrderDto confirmOrderDto) {
        Integer payType = confirmOrderDto.getPayType();
        BigDecimal money = confirmOrderDto.getMoney();
        Long id = confirmOrderDto.getId();
        // 修改状态为1
        OrderProduct orderProduct = orderProductMapper.loadById(id);
        orderProduct.setState(1);
        orderProduct.setPaySn(payType.toString());
        orderProduct.setPrice(money);
        orderProductMapper.update(orderProduct);
        // 保存操作日志
        ProductOrderLog productOrderLog = new ProductOrderLog();
        productOrderLog.setNote("确认订单！");
        productOrderLog.setProductOrderId(id);
        productOrderLog.setEmpId(orderProduct.getUserId());
    }

    @Override
    public void cancel(Long id) {
        // 修改状态为-1
        OrderProduct orderProduct = orderProductMapper.loadById(id);
        orderProduct.setState(-1);
        orderProductMapper.update(orderProduct);
        // 保存操作日志
        ProductOrderLog productOrderLog = new ProductOrderLog();
        productOrderLog.setNote("确认取消！");
        productOrderLog.setProductOrderId(id);
        productOrderLog.setEmpId(orderProduct.getUserId());
        // 删除对应的订单详情
        orderProductDetailMapper.removeByProductId(orderProduct.getId());
    }


}
